#!/usr/bin/env python
from maputility2 import containers
from maputility2.skyutils import fastOpen

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Healpix Arithmatic Tools')
    parser.add_argument("f1", type=str, help='infile')
    parser.add_argument("const", type=float, help='constant')
    parser.add_argument("fout", type=str, help='fileout = file ops const')
    parser.add_argument("ops", type=str, help='operators +|-|*|/|^')
    args = parser.parse_args()

    m1 = fastOpen(args.f1, verbose=False, container=containers.PixMap)

    if args.ops == '+':
        m1 += args.const
        m1.writeHistory('{} + {}'.format(args.f1, args.const), origin='HpxCArith')
    elif args.ops == '-':
        m1 -= args.const
        m1.writeHistory('{} - {}'.format(args.f1, args.const), origin='HpxCArith')
    elif args.ops == '*':
        m1 *= args.const
        m1.writeHistory('{} * {}'.format(args.f1, args.const), origin='HpxCArith')
    elif args.ops == '/':
        m1 /= args.const
        m1.writeHistory('{} / {}'.format(args.f1, args.const), origin='HpxCArith')
        zero_fil = (~np.isfinite(m1.mapdata)) | (np.abs(m1.mapdata)<args.zero)
        m1.mapdata[zero_fil] = 0.
    elif args.ops == '^':
        m1 **= args.const
        m1.writeHistory('{} ^ {}'.format(args.f1, args.const), origin='HpxCArith')
    else:
        raise NotImplementedError('Unsupported operator ({}), please use "+|-|*|/|^"')

    m1.saveMap(args.fout)
